package it.tic.sbibm.persistence.lov;

import it.tic.sbibm.persistence.config.SbConfiguration;

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public abstract class AbstractListOfValues implements Serializable {
	private static final long serialVersionUID = -4945571869019542331L;

	// Contains in a single static allocation all list of values of the
	// application, to avoid reloading it from DB after the first time
	private static Map<String, Map> valuesLists;

	// Key of properties in a prop file
	abstract protected String valuesListsCommonKey();

	public Map<String, String> getValuesList() {
		if (valuesLists == null) valuesLists = new HashMap<String, Map>();
		Map<String, String> valuesList = valuesLists.get(this.valuesListsCommonKey());
		if (valuesList == null) {
			valuesList = new LinkedHashMap<String, String>();
			Map<String, String> lista = SbConfiguration.getListOfValues(this.valuesListsCommonKey());
			valuesList.putAll(lista);
			valuesLists.put(this.valuesListsCommonKey(), lista);
		}
		return valuesList;
	}

	public String getValueByKey(String key) {
		if (this.getValuesList() == null) return null;
		return this.getValuesList().get(key);
	}
}
